function createXHR() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    return new ActiveXObject("Msxml2.XMLHTTP");
}

// opts={url:xx,async:xx,success:function(){}}
function ajax(opts) {
    opts.async = opts.async == undefined ? true : opts.async //默认为true

    let xhr = createXHR();
    xhr.open("get", opts.url+"?"+getParams(opts.data), opts.async);
    xhr.send(null);


    if (opts.async == true) {
        //异步
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                result();
            }
        }
    } else {
        //同步
        result();
    }
    function result() {
        if (xhr.status == 200) {
            if (typeof (opts.success) == "function") {
                if (opts.type=="json") {
                    opts.success(JSON.parse(xhr.responseText));
                }else{
                    opts.success(xhr.responseText);
                }
            }
        }
    }
}

// obj= {page:1,size=20} --> "?page=1&size=20&"
function getParams(obj) {
    var str="";
    for(let key in obj){
        str+=key+"="+obj[key]+"&"
    }
    return str.substr(0,str.length-1);
}
